package com.ic.loan.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.ColorUtils
import com.ic.loan.R
import kotlinx.android.synthetic.main.layout_bottom_bar.view.*

class BottomBar : LinearLayoutCompat {
    private lateinit var fragmentManager: FragmentManager
    private var fragmentContainerId: Int? = null
    private lateinit var fragmentList: MutableList<Class<Fragment>>
    private val bottomItemViewList: MutableList<View> = mutableListOf()
    private lateinit var resourceList: MutableList<ResourceBean>
    private var clickItemCallback: ((position: Int) -> Unit)? = null
    private var currentIndex: Int? = null
    private var textDefaultColor: Int? = null
    private var textSelectColor: Int? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        orientation = HORIZONTAL
    }

    fun setResource(
        resourceList: MutableList<ResourceBean>,
        textDefaultColor: Int,
        textSelectColor: Int,
        defaultSelectIndex: Int = 0
    ) {
        this.resourceList = resourceList
        this.textDefaultColor = textDefaultColor
        this.textSelectColor = textSelectColor
        currentIndex = defaultSelectIndex
        resourceList.forEachIndexed { index, it ->
            val view = LayoutInflater.from(context).inflate(R.layout.layout_bottom_bar, null)
            view.layoutParams = LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT)?.apply {
                weight = 1f
            }
            if (defaultSelectIndex == index) {
                view.image.setImageResource(it.selectImageResource)
                view.title.setTextColor(ColorUtils.getColor(textSelectColor))
            } else {
                view.image.setImageResource(it.defaultImageResource)
                view.title.setTextColor(ColorUtils.getColor(textDefaultColor))
            }
            view.title.text = it.title
            view.setOnClickListener {
                if (currentIndex == index) {
                    return@setOnClickListener
                }
                currentIndex = index
                selectItem(index)
                clickItemCallback?.invoke(index)
                showFragment(index)
            }
            bottomItemViewList.add(view)
            addView(view)
        }
        currentIndex?.let {
            showFragment(it)
        }
    }

    private fun showFragment(index: Int) {
        var transaction = fragmentManager.beginTransaction()
        fragmentList.forEach {
            val fragment = fragmentManager.findFragmentByTag(it.simpleName)
            if (fragment != null) {
                transaction.hide(fragment)
            }
        }
        val fragment = fragmentManager.findFragmentByTag(fragmentList[index].simpleName)
        if (fragment == null) {
            fragmentContainerId?.let {
                transaction.add(
                    it,
                    fragmentList[index],
                    null,
                    fragmentList[index].simpleName
                )
            }
        } else {
            transaction.show(fragment)
        }
        transaction.commit()
    }


    fun bindFragment(
        fragmentContainerId: Int,
        fragmentManager: FragmentManager,
        fragmentList: MutableList<Class<Fragment>>
    ) {
        this.fragmentContainerId = fragmentContainerId
        this.fragmentManager = fragmentManager
        this.fragmentList = fragmentList
    }

    private fun selectItem(position: Int) {
        bottomItemViewList.forEachIndexed { index, view ->
            if (position == index) {
                view.image.setImageResource(resourceList[index].selectImageResource)
                textSelectColor?.let { view.title.setTextColor(ColorUtils.getColor(it)) }
            } else {
                view.image.setImageResource(resourceList[index].defaultImageResource)
                textDefaultColor?.let { view.title.setTextColor(ColorUtils.getColor(it)) }
            }
        }
    }

    data class ResourceBean(
        var title: String,
        var defaultImageResource: Int,
        var selectImageResource: Int
    )

}